<template>
	<view>
		<view class="open">
			<text>蓝牙</text>
			<u-switch v-model="state" @change="stateChange"></u-switch>
		</view>
		
		<view class="">
			<button @click="test1">测试1</button>
			<view class="">
				{{ platform }}
			</view>
			<button @click="test2">打开系统蓝牙</button>
			<button @click="test3">获取系统蓝牙状态</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				state: false,
				platform: '',
			}
		},
		methods: {
			test1() {
				const data = {
					res: uni.$tools.platform(),
					platform: uni.getSystemInfoSync().platform,
					uniPlatform: uni.getSystemInfoSync().uniPlatform,
				}
				this.platform = JSON.stringify(data)				
			},
			test2() {
				uni.$bluetooth.openSystemBluetooth()
			},
			test3 () {
				console.log('系统蓝牙状态', uni.$store.state.systemBluetoothState);
			},
			/**
			 * 监听蓝牙开关状态
			 */
			stateChange(e) {
				// #ifndef APP-PLUS
				this.state = false
				uni.$tools.toast('蓝牙API示例仅支持APP')
				// #endif
				
				// #ifdef APP-PLUS
				if (!uni.$store.state.systemBluetoothState) {
					this.state = false
					uni.$tools.toast('请先开启系统蓝牙', () => {
						uni.$bluetooth.openSystemBluetooth()
					})
				}
				if (e) {
					uni.$bluetooth.searchBluetooth()
				}
				// #endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: rgb(250, 250, 250);
	}
	.open {
		display: flex;
		justify-content: space-between;
		width: 94%;
		margin: 30rpx auto 0;
		border-radius: 16rpx;
		height: 100rpx;
		background: #fff;
		align-items: center;
		padding: 26rpx;
		box-sizing: border-box;
	}
</style>
